import os
import psycopg2
from QRoute import QRoute
from PyQt5 import QtWidgets
from PyQt5.QtCore import QDateTime, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QMainWindow, QStatusBar, QLabel, QAction
from ui import DashboardWindow
from Page import Order

class Widget(QMainWindow, QRoute.RouteSignal):
    def __init__(self, parent=None):
        super(QWidget, self).__init__()
        self.setting = parent[0]
        self.unit = parent[1]
        self.user = parent[2]
        self.account = parent[3]
        self.dictionary = parent[4]
        self.gui_orderDialog = Order.Widget()
        self.ui_Dashboard = DashboardWindow.Ui_MainWindow()
        self.ui_Dashboard.setupUi(self)
        self.ui_Dashboard.retranslateUi(self)
        self.statusChange()

    def timer_timeout(self):  # 定时中断相应
        curTime = QDateTime.currentDateTime()  # 获取当前时间
        timePlay = curTime.toString('yyyy年MM月dd日 dddd hh:mm:ss')
        self.ui_Dashboard.dateTimeLabel.setText(timePlay)

    def statusChange(self):
        user = str(self.user['realname']) + '(' + str(self.user['email']) + ')'
        self.statusBarLabel = QStatusBar()
        self.statusBarLabel.addWidget(QLabel('{:<30}'.format('当前用户：' + user)))
        self.statusBarLabel.addWidget(QLabel('{:^30}'.format('账户总市值：￥1585214.05')))
        self.statusBarLabel.addWidget(QLabel('{:^30}'.format('持仓证券市值：￥1585214.05')))
        self.statusBarLabel.addWidget(QLabel('{:^30}'.format('可用资金：￥1585214.05')))
        self.statusBarLabel.addWidget(QLabel('{:^30}'.format('可取资金：￥1585214.05')))
        self.statusBarLabel.addWidget(self.ui_Dashboard.dateTimeLabel)
        self.timer = QTimer()  # 创建定时器
        self.timer.start(1000)  # 定时周期1000ms
        self.timer.timeout.connect(self.timer_timeout)
        self.setStatusBar(self.statusBarLabel)

    def showNewOrderDialog(self):
        self.gui_orderDialog.setting = self.setting
        self.gui_orderDialog.unit = self.unit
        self.gui_orderDialog.user = self.user
        self.gui_orderDialog.account = self.account
        self.gui_orderDialog.dictionary = self.dictionary
        self.gui_orderDialog.setupDefault()
        self.gui_orderDialog.show()

    def showAllOrderList(self):
        print('showAllOrderList')

    def showFinishOrderList(self):
        print('showFinishOrderList')

    def showRunOrderList(self):
        print('showRunOrderList')